﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;

public partial class Users_Create : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //PermissionHelper hh = new PermissionHelper();
            //Session["Permission"] = hh.GetPermission(EUserRole.superadmin);
            btnCreate.Visible = false;
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsUserEdit)
                {
                    btnCreate.Visible = true;
                    txtCurrentIntegral.Text = "6";
                }


                CompanyBLL bll = new CompanyBLL();
                ddlCompany.DataSource = bll.GetAllCompany();
                ddlCompany.DataTextField = "CompanyName";
                ddlCompany.DataValueField = "CompanyID";
                ddlCompany.DataBind();

                AdminBLL abll = new AdminBLL();
                ddlUserType.DataSource = abll.GetAllUserType();
                ddlUserType.DataTextField = "Name";
                ddlUserType.DataValueField = "ID";
                ddlUserType.DataBind();

                ddlUserRole.DataSource = abll.GetAllUserRole();
                ddlUserRole.DataTextField = "Name";
                ddlUserRole.DataValueField = "ID";
                ddlUserRole.DataBind();

                if (Request.Params["cid"] != null)
                {
                    LoadCompany(Convert.ToInt32(Request.Params["cid"]));
                }
            }

        }
    }

    void LoadCompany(int cid)
    {
        CompanyBLL bll = new CompanyBLL();
        Company company = bll.GetCompany(cid);
        if (company != null)
        {
            ddlCompany.SelectedValue = company.CompanyID.ToString();
            ddlCompany.Enabled = false;
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //to do:前后端的数据验证
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission != null)
            {
                InternalManagementSystem.DAO.Models.Staff user = new InternalManagementSystem.DAO.Models.Staff();

                user.UserName = txtUserName.Text;
                user.CompanyID = Convert.ToInt32(ddlCompany.SelectedValue);
                user.Credential = txtCredential.Text;
                user.CurrentIntegral = Convert.ToInt32(txtCurrentIntegral.Text);
                user.IDCard = txtIDCard.Text;
                user.RoleID = Convert.ToInt32(ddlUserRole.SelectedValue);//用户角色
                user.UserTypeID = Convert.ToInt32(ddlUserType.SelectedValue);//用户种类
                user.Password = "1234";//默认密码
 

                if (rbmale.Checked)
                {
                    user.Sex = 1;
                }

                if (rbfemale.Checked)
                {
                    user.Sex = 0;
                }

                user.IsSafetyEducation = cbSafe.Checked ? 1 : 0;
                user.IsFine = cbfine.Checked ? 1 : 0;

                user.IsAutoDeductPoints = cbAutoDeduct.Checked ? 1 : 0;
                user.AutoDeductPeriod = string.IsNullOrEmpty(txtAutoDeductPeriod.Text) ? 0 : Convert.ToInt32(txtAutoDeductPeriod.Text);
                user.AutoDeductPoints = string.IsNullOrEmpty(txtAutoDeductPoints.Text) ? 0 : Convert.ToInt32(txtAutoDeductPoints.Text);

                UserBLL bll = new UserBLL(permission);

                bll.AddUser(user);

                if (Request.Params["cid"] != null)
                {
                    Response.Write("<script> alert( '提交成功！ ');location= 'Index.aspx?cid=" + Request.Params["cid"]+"'</script> ");
                }
                else
                {
                    Response.Write("<script> alert( '提交成功！ ');location= 'Index.aspx'</script> ");
                }

              
            }
        }

    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        if (Request.Params["cid"] != null)
        {
            Response.Redirect("Index.aspx?cid=" + Request.Params["cid"]);
        }
        else
        {
            Response.Redirect("Index.aspx");
        }

        
    }
}
